package com.wz.wzbubbleai.apdater;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wz.wzbubbleai.R;
import com.wz.wzbubbleai.apdater.Item.BaseListItem;
import com.wz.wzbubbleai.apdater.Item.BubbleTextItem;
import com.wz.wzbubbleai.apdater.Item.UserTextItem;
import com.wz.wzbubbleai.apdater.ViewHolder.BubbleTextHolder;
import com.wz.wzbubbleai.apdater.ViewHolder.UserTextHolder;

import java.util.List;

public class ChatAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public ChatAdapter(Context context){
        this.mInflater = LayoutInflater.from(context);
    }

    private List<BaseListItem> list;


    public void setList(List<BaseListItem> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        if(list != null){
            return list.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (list != null && position < list.size()) {
            return list.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        int type = getItemViewType(position);
        switch (type) {
            case BaseListItem.TYPE_BUBBLE_TEXT: {
                BubbleTextHolder holder = null;
                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.bubble_text_layout, null);
                    holder = new BubbleTextHolder();
                    holder.setTextView((TextView) convertView.findViewById(R.id.bubble_text));
                    convertView.setTag(holder);
                } else {
                    holder = (BubbleTextHolder) convertView.getTag();
                }
                holder.getTextView().setText(((BubbleTextItem)list.get(position)).getDetail());
                break;
            }

            case BaseListItem.TYPE_USER_TEXT: {
                UserTextHolder holder = null;
                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.user_text_layout, null);
                    holder = new UserTextHolder();
                    holder.setTextView((TextView) convertView.findViewById(R.id.user_text));
                    convertView.setTag(holder);
                } else {
                    holder = (UserTextHolder) convertView.getTag();
                }
                holder.getTextView().setText(((UserTextItem)list.get(position)).getDetail());
                break;
            }

            default:
                break;
        }

        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        if (list != null && position < list.size()) {
            return list.get(position).getType();
        }

        return super.getItemViewType(position);
    }

    @Override
    public int getViewTypeCount() {
        return BaseListItem.TYPE_NUMS;
    }
}
